package com.sunmouse.toolkit.activity;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunmouse.toolkit.BaseActivity;
import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.util.PageUtil;


/**
 * Created by Kvin on 2017/5/25.
 */
public class ToolbarActivity extends BaseActivity {

    TextView titleTv;
    Toolbar toolbar;
    RelativeLayout contentContainer;
    FloatingActionButton fab;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.setContentView(R.layout.activity_toolbar);
        this.titleTv = (TextView) this.findViewById(R.id.title_tv);
        this.toolbar = (Toolbar) this.findViewById(R.id.toolbar);
        this.toolbar.setTitle("");

        this.contentContainer = (RelativeLayout) this.findViewById(R.id.fragment_container);
        this.fab = (FloatingActionButton) this.findViewById(R.id.fab);
        /**
         * bind toolbar as new actionbar
         */
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

    }



    /**
     * get title
     */
    private String getTitle(int index) {
        String title = "";
        switch (index) {
            case PageUtil.Index.MAIN:
//                title = getString(R.string.title_collection);
                break;

        }

        return title;
    }


    /**
     * set content
     */
    public void setContentView(@LayoutRes int resId) {
        View v = LayoutInflater.from(this).inflate(resId, null);
        this.contentContainer.addView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

    }

    public void setTitle(String msg) {
        this.titleTv.setText(msg);
    }

    public void setTitle(CharSequence msg) {
        this.setTitle(msg.toString());
    }

    protected void setFabVisible(boolean visible) {
        if (visible) {
            this.fab.setVisibility(View.VISIBLE);
        } else {
            this.fab.setVisibility(View.GONE);
        }
    }

    protected boolean isFabShowing() {
        if (fab.getVisibility() == View.VISIBLE) return true;
        return false;
    }

    /**
     * set click listener
     *
     * @param listener
     */
    protected void setFabClickListener(View.OnClickListener listener) {
        this.fab.setOnClickListener(listener);
    }

    /**
     * return  toolbar
     */
    protected Toolbar getToolbar() {
        return toolbar;
    }

    /**
     * default nav click
     */
    protected final View.OnClickListener mDefaultNavClick=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getActivity().finish();
        }
    };




    /**
     * on navigation click listener
     */
    public class OnNavClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            getActivity().finish();
        }
    }


}
